package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoAppraise;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;

/**
 * 专家技术评估表操作类
 * 
 * @author Administrator
 *
 */
@Alias(value = "appraise")
public class AppraiseUI extends UIFacade {

	/**
	 * 增加或更改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate(String appraiseList) {
		Transaction transaction = new Transaction();
		List<String> scids = new ArrayList<>();
		String project_id = null;
		if (!StringUtils.isEmpty(appraiseList)) {
			List<Map<String, Object>> array = JSON.parseObject(appraiseList,
					new TypeReference<List<Map<String, Object>>>() {
			});
			for (Map<String, Object> list : array) {
				System.out.println(list);
				EaoAppraise appraise = new EaoAppraise();
				appraise.setValue(list);
				project_id = appraise.getProjectId();
				String id = appraise.getAppraiseId();
				if (StringUtils.isEmpty(id)) {
					// 添加
					appraise.setAppraiseId(UUID.randomUUID().toString());
					transaction.insert(appraise);
					scids.add(appraise.getAppraiseId());
				} else {
					// 更新
					scids.add(id);
					DataTable dt = new Q(" select * from eao_appraise where AppraiseId = ?", id).fetch();
					EaoAppraise entity = new EaoAppraise();
					entity.setValue(dt.get(0));
					entity.setValue(list);
					transaction.update(entity);
				}
			}
		} else {
			f("请传值");
		}

		DataTable dt = new Q(" select * from eao_appraise where ProjectId = ?", project_id).fetch();
		for (DataRow dataRow : dt) {
			EaoAppraise entity = new EaoAppraise();
			entity.setValue(dataRow);
			Boolean del = true;
			for (String scid : scids) {
				if (entity.getAppraiseId().equals(scid)) {
					del = false;
				}
			}
			if (del) {
				transaction.delete(entity);
			}
		}
		if (transaction.commit()) {
			s("操作成功");
		} else {
			f("操作失败");
		}
	}
}